# -*- coding: utf-8 -*-
"""
    PBAR_MakeConsolidatedMergedMarkerFiles.py
    
    Make files each containing the merged merged markers for each combination of parameters.

Created on Tue May 20 10:27:08 2014

@author: jkwong
"""

import glob, os, gc, objgraph, cPickle, time, copy, shutil
import numpy as np
import PBAR_Zspec, PBAR_Cargo
import matplotlib.pyplot as plt
from matplotlib import mpl
from scipy import ndimage
reload(PBAR_Zspec)

dataPath = r'E:\PBAR\data4\BasicScansStandardWidthPickle'
dataOutputPath = r'E:\PBAR\data4\HighZFinderMarkers'

# make different size windows
# x and y
windowList = []
windowList.append(np.array((3, 1)))
windowList.append(np.array((5, 1)))
windowList.append(np.array((7, 1)))
windowList.append(np.array((4, 2)))
windowList.append(np.array((7, 2)))
# thresholds
discrimThresholdList = np.array((0, 2))
# fractions
fractionPixelsList = np.array((0.8, 1.0))

tolerance = [3, 1]

datasetDescription = PBAR_Zspec.ReadCargoDataDescriptionFile(r'E:\PBAR\data4\CargoSet2.txt')

# loop through each combination of parameters
for windowIndex, window in enumerate(windowList):
    for discrimThresholdIndex, discrimThreshold in enumerate(discrimThresholdList):
        for fractionPixelsIndex, fractionPixels in enumerate(fractionPixelsList):

            print('Working on %d, %d, %d' %(windowIndex, discrimThresholdIndex, fractionPixelsIndex))

            highZMarkersList = []
#            highZMergedMarkersList = []
            highZMergedMergedMarkersList = []
            performanceList = []
            
            for datasetIndex, datasetDescript in enumerate(datasetDescription['scanID']):
                print('%d, %s' %(datasetIndex, datasetDescript))
                zspecScanNumber = np.int(datasetDescript)
            
                # Read Zspec
                filenameZspec = '%s-FDFC-All_SW.dat' %datasetDescription['scanID'][datasetIndex]
                fullFilenameZspec = os.path.join(dataPath, filenameZspec)
                
                # Read Cargo
                filenameCargo = 'PBAR-%s.cargoimageSW.dat' %datasetDescription['dataFile'][datasetIndex]
                fullFilenameCargo = os.path.join(dataPath, filenameCargo)
            
                # Read in marker files
                filenameMarker = filenameCargo.replace('cargoimageSW.dat', 'cargomarkerSW')
                fullFilenameMarker = fullFilenameCargo.replace('cargoimageSW.dat', 'cargomarkerSW')
                
                # some don't have marker files
                if os.path.exists(fullFilenameMarker):
                    markerStandardWidth0 = PBAR_Cargo.ReadCargoMarker(fullFilenameMarker)
                else:
                    markerStandardWidth0 = []
                
                fullFilenamePotential = \
                    fullFilenameMarker.replace('cargomarkerSW', \
                        '%d_%d_%d_%3.2f.potential' %(window[0], window[1], discrimThreshold, fractionPixels) )
#                print('Working on %s' %fullFilenamePotential)
                
                # load original high markers
                with open(fullFilenamePotential, 'rb') as fid:
                    print('Load Original Markers: %s' %fullFilenamePotential)
                    highZMarkersList.append(cPickle.load(fid))
#                    
#                # load the merged markers
#                fullFilenameMarkerMerged = fullFilenamePotential.replace('potential', 'highZMerged')
#                with open(fullFilenameMarkerMerged, 'rb') as fid:
#                    print('Load Merged Markers: %s' %fullFilenameMarkerMerged)
#                    highZMergedMarkersList = cPickle.load(fid)
                    
                # read merged merged markers
                fullFilenameMarkerMerged = fullFilenamePotential.replace('potential', 'highZMergedMerged')
                with open(fullFilenameMarkerMerged, 'rb') as fid:
                    print('Load Merged Merged Markers: %s' %fullFilenameMarkerMerged)
                    highZMergedMergedMarkersList.append(cPickle.load(fid))
                    
                # read the merged merged marker performance
                fullFilenamePerformance = fullFilenamePotential.replace('potential', 'performance')
                with open(fullFilenamePerformance, 'rb') as fid:
                    print('Write: %s' %fullFilenamePerformance)
                    performanceList.append(cPickle.load(fid))

            # save lists to file
            fullFilename = os.path.join(dataOutputPath, '%d_%d_%d_%3.2f.highz' %(window[0], window[1], discrimThreshold, fractionPixels) )
            with open(fullFilename, 'wb') as fid:
                print('Writing: %s' %fullFilename)
                cPickle.dump(highZMarkersList, fid, 2)
                
            fullFilename = os.path.join(dataOutputPath, '%d_%d_%d_%3.2f.highzmergedmerged' %(window[0], window[1], discrimThreshold, fractionPixels) )
            with open(fullFilename, 'wb') as fid:
                print('Writing: %s' %fullFilename)
                cPickle.dump(highZMergedMergedMarkersList, fid, 2)
                
            fullFilename = os.path.join(dataOutputPath, '%d_%d_%d_%3.2f.performance' %(window[0], window[1], discrimThreshold, fractionPixels) )
            with open(fullFilename, 'wb') as fid:
                print('Writing: %s' %fullFilename)
                cPickle.dump(performanceList, fid, 2)
                